package com.atydh.gulimall.produck.Exception;

import com.atydh.common.exception.BizCodeEnume;
import com.atydh.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;


@Slf4j
@RestControllerAdvice(basePackages = "com.atydh.gulimall.produck.controller")
public class ProduckException {

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public R handleProduckException(MethodArgumentNotValidException M) {
        log.error("数据出现问题",M.getMessage());
        BindingResult bindingResult = M.getBindingResult();
        HashMap<String, String> errorMap = new HashMap<>();
        bindingResult.getFieldErrors().forEach(fifieldError -> {
            errorMap.put(fifieldError.getField(),fifieldError.getDefaultMessage());
        });
        return R.error(BizCodeEnume.VAILD_EXCEPTION.getCode(),BizCodeEnume.VAILD_EXCEPTION.getMsg()).put("data",errorMap);
    }
}
